package com.example.oss.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.oss.entity.Course;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface CourseMapper extends BaseMapper<Course> {

    //更新库存，当版本号也一致时，才进行更新操作，库存更新，版本号也要更新
    @Update("update t_course set amount = #{amount}, _version = _version + 1 " +
            "where id = #{id} and _version = #{version} ")
    int reduceAmount(Course course);

    //查库存，查询的时候要把版本号也查出来
    @Select("select id, amount, _version from t_course where id = #{courseId} and amount > 0")
    Course selectAmount(int courseId);


    //小于初始值50才自增
    @Update("update t_course set amount = amount + 1 where id = #{courseId} and amount < 50")
    void increaseAmount(int courseId);
}
